#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<vector>
using namespace std;

class Solution {
public:
    vector<vector<string>> res;
    void dfs(vector<string>& board, int n, int pos)
    {
        if (pos == n)
        {
            res.push_back(board);
            return;
        }
        for (int i = 0; i < n; ++i)
        {
            board[pos][i] = 'Q';
            if (isvalid(board, pos, i))
            {
                dfs(board, n, pos + 1);
            }
            board[pos][i] = '.';
        }
    }
    vector<vector<string>> solveNQueens(int n) {
        string temp;
        for (int i = 0; i < n; ++i)
            temp += '.';
        vector<string> board(n, temp);
        dfs(board, n, 0);
        return res;
    }
    bool isvalid(vector<string> board, int row, int colum)
    {
        int n = board.size();
        for (int i = 0; i < n; ++i)
        {
            if (i == row) continue;
            if (board[i][colum] == 'Q') return false;
        }

        int temprow = row - 1, tempcolum = colum - 1;
        while (temprow >= 0 && tempcolum >= 0)
            if (board[temprow--][tempcolum--] == 'Q') return false;

        temprow = row - 1, tempcolum = colum + 1;
        while (temprow >= 0 && tempcolum < n)
            if (board[temprow--][tempcolum++] == 'Q') return false;
        return true;
    }

    vector<vector<string>> res;
    bool checkCol[10];
    bool checkDig1[20];
    bool checkDig2[20];
    void dfs(vector<string>& board, int n, int pos)
    {
        if (pos == n)
        {
            res.push_back(board);
            return;
        }
        for (int i = 0; i < n; ++i)
        {
            if (!checkCol[i] && !checkDig1[pos - i + n] && !checkDig2[pos + i])
            {
                board[pos][i] = 'Q';
                checkCol[i] = checkDig1[pos - i + n] = checkDig2[pos + i] = true;
                dfs(board, n, pos + 1);
                board[pos][i] = '.';
                checkCol[i] = checkDig1[pos - i + n] = checkDig2[pos + i] = false;
            }
        }
    }
    vector<vector<string>> solveNQueens(int n) {
        string temp;
        for (int i = 0; i < n; ++i)
            temp += '.';
        vector<string> board(n, temp);
        dfs(board, n, 0);
        return res;
    }
};